home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / Text3D / text3d.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  12.3 KB  |  379 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Text3D.cpp
  3. //
  4. // Desc: Example code showing how to do text in a Direct3D scene.
  5. //
  6. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <commdlg.h>
  11. #include <math.h>
  12. #include <tchar.h>
  13. #include <stdio.h>
  14. #include <D3DX8.h>
  15. #include "D3DApp.h"
  16. #include "D3DFont.h"
  17. #include "D3DUtil.h"
  18. #include "DXUtil.h"
  19. #include "resource.h"
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Name: class CMyD3DApplication
  23. // Desc: Application class. The base class (CD3DApplication) provides the 
  24. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  25. //       adds functionality specific to this sample program.
  26. //-----------------------------------------------------------------------------
  27. class CMyD3DApplication : public CD3DApplication
  28. {
  29.     CD3DFont*     m_pFont;
  30.     CD3DFont*     m_pStatsFont;
  31.     LPD3DXMESH    m_pMesh3DText;
  32.  
  33.     TCHAR         m_strFont[100];
  34.     DWORD         m_dwFontSize;
  35.  
  36.     D3DXMATRIX    m_matObj1;
  37.     D3DXMATRIX    m_matObj2;
  38.  
  39. protected:
  40.     HRESULT OneTimeSceneInit();
  41.     HRESULT InitDeviceObjects();
  42.     HRESULT RestoreDeviceObjects();
  43.     HRESULT InvalidateDeviceObjects();
  44.     HRESULT DeleteDeviceObjects();
  45.     HRESULT Render();
  46.     HRESULT FrameMove();
  47.     HRESULT FinalCleanup();
  48.     HRESULT CreateD3DXTextMesh();
  49.  
  50. public:
  51.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  52.     CMyD3DApplication();
  53. };
  54.  
  55.  
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Name: WinMain()
  60. // Desc: Entry point to the program. Initializes everything, and goes into a
  61. //       message-processing loop. Idle time is used to render the scene.
  62. //-----------------------------------------------------------------------------
  63. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  64. {
  65.     CMyD3DApplication d3dApp;
  66.  
  67.     if( FAILED( d3dApp.Create( hInst ) ) )
  68.         return 0;
  69.  
  70.     return d3dApp.Run();
  71. }
  72.  
  73.  
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Name: CMyD3DApplication()
  78. // Desc: Application constructor. Sets attributes for the app.
  79. //-----------------------------------------------------------------------------
  80. CMyD3DApplication::CMyD3DApplication()
  81. {
  82.     m_strWindowTitle    = _T("Text3D: Text in a 3D scene");
  83.     m_bUseDepthBuffer   = TRUE;
  84.     m_pMesh3DText       = NULL;
  85.  
  86.     // Create fonts
  87.     lstrcpy( m_strFont, _T("Arial") );
  88.     m_dwFontSize  = 18;
  89.     m_pFont       = new CD3DFont( m_strFont, m_dwFontSize );
  90.     m_pStatsFont  = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  91. }
  92.  
  93.  
  94.  
  95.  
  96. //-----------------------------------------------------------------------------
  97. // Name: OneTimeSceneInit()
  98. // Desc: Called during initial app startup, this function performs all the
  99. //       permanent initialization.
  100. //-----------------------------------------------------------------------------
  101. HRESULT CMyD3DApplication::OneTimeSceneInit()
  102. {
  103.     return S_OK;
  104. }
  105.  
  106.  
  107.  
  108.  
  109. //-----------------------------------------------------------------------------
  110. // Name: FrameMove()
  111. // Desc: Called once per frame, the call is the entry point for animating
  112. //       the scene.
  113. //-----------------------------------------------------------------------------
  114. HRESULT CMyD3DApplication::FrameMove()
  115. {
  116.     // Setup five rotation matrices (for rotating text strings)
  117.     D3DXMatrixRotationAxis( &m_matObj1, &D3DXVECTOR3(1.0f,2.0f,0.0f), m_fTime/2.0f  );
  118.     D3DXMatrixRotationAxis( &m_matObj2, &D3DXVECTOR3(2.0f,1.0f,0.0f), m_fTime/2.0f  );
  119.  
  120.     // Add some translational values to the matrices
  121.     m_matObj1._41 = 1.0f;   m_matObj1._42 = 6.0f;   m_matObj1._43 = 20.0f; 
  122.     m_matObj2._41 = -4.0f;  m_matObj2._42 = -1.0f;  m_matObj2._43 = 0.0f; 
  123.  
  124.     return S_OK;
  125. }
  126.  
  127.  
  128.  
  129.  
  130. //-----------------------------------------------------------------------------
  131. // Name: Render()
  132. // Desc: Called once per frame, the call is the entry point for 3d
  133. //       rendering. This function sets up render states, clears the
  134. //       viewport, and renders the scene.
  135. //-----------------------------------------------------------------------------
  136. HRESULT CMyD3DApplication::Render()
  137. {
  138.     D3DMATERIAL8 mtrl;
  139.  
  140.     // Clear the viewport
  141.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
  142.  
  143.     // Begin the scene 
  144.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  145.     {
  146.         // Draw CD3DFont in 2D (red)
  147.         m_pFont->DrawText( 60, 100, D3DCOLOR_ARGB(255,255,0,0), _T("CD3DFont::DrawText") );
  148.  
  149.         // Draw CD3DFont in 3D (green)
  150.         D3DUtil_InitMaterial( mtrl, 0.0f, 1.0f, 0.0f );
  151.         m_pd3dDevice->SetMaterial( &mtrl );
  152.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj1 );
  153.         m_pFont->Render3DText( _T("CD3DFont::Render3DText"), D3DFONT_CENTERED|D3DFONT_TWOSIDED|D3DFONT_FILTERED );
  154.  
  155.         // Draw D3DXFont mesh in 3D (blue)
  156.         if( m_pMesh3DText != NULL )
  157.         {
  158.             D3DUtil_InitMaterial( mtrl, 0.0f, 0.0f, 1.0f );
  159.             m_pd3dDevice->SetMaterial( &mtrl );
  160.             m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matObj2 );
  161.             m_pMesh3DText->DrawSubset(0);
  162.         }
  163.  
  164.         // Show frame rate
  165.         m_pStatsFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  166.         m_pStatsFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  167.  
  168.         // End the scene.
  169.         m_pd3dDevice->EndScene();
  170.     }
  171.  
  172.     return S_OK;
  173. }
  174.  
  175. //-----------------------------------------------------------------------------
  176. // Name: InitDeviceObjects()
  177. // Desc: Initialize scene objects.
  178. //-----------------------------------------------------------------------------
  179. HRESULT CMyD3DApplication::InitDeviceObjects()
  180. {
  181.     // Restore the fonts
  182.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  183.     m_pStatsFont->InitDeviceObjects( m_pd3dDevice );
  184.  
  185.     return S_OK;
  186. }
  187.  
  188.  
  189.  
  190.  
  191. //-----------------------------------------------------------------------------
  192. // Name: RestoreDeviceObjects()
  193. // Desc: Initialize scene objects.
  194. //-----------------------------------------------------------------------------
  195. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  196. {
  197.     // Restore the fonts
  198.     m_pFont->RestoreDeviceObjects();
  199.     m_pStatsFont->RestoreDeviceObjects();
  200.  
  201.     // Restore the textures
  202.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  203.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  204.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  205.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  206.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  207.  
  208.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  209.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
  210.     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
  211.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  212.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x80808080);
  213.     D3DLIGHT8 light;
  214.     D3DUtil_InitLight(light, D3DLIGHT_DIRECTIONAL, 10.0f, -10.0f, 10.0f);
  215.     m_pd3dDevice->SetLight(0, &light );
  216.     m_pd3dDevice->LightEnable(0, TRUE);
  217.  
  218.     // Set the transform matrices
  219.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f,-5.0f,-10.0f );
  220.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f,  0.0f );
  221.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f,  0.0f );
  222.     D3DXMATRIX  matWorld, matView, matProj;
  223.  
  224.     D3DXMatrixIdentity( &matWorld );
  225.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  226.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  227.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
  228.  
  229.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  230.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  231.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  232.  
  233.     if( FAILED( CreateD3DXTextMesh() ) )
  234.         return E_FAIL;
  235.  
  236.     return S_OK;
  237. }
  238.  
  239.  
  240.  
  241.  
  242. //-----------------------------------------------------------------------------
  243. // Name: CreateD3DXTextMesh()
  244. // Desc: 
  245. //-----------------------------------------------------------------------------
  246. HRESULT CMyD3DApplication::CreateD3DXTextMesh()
  247. {
  248.     HRESULT hr;
  249.     HDC hdc = CreateCompatibleDC( NULL );
  250.     HFONT hFont;
  251.     HFONT hFontOld;
  252.     LOGFONT lf;
  253.  
  254.     SAFE_RELEASE(m_pMesh3DText);
  255.     ZeroMemory(&lf, sizeof(lf));
  256.     lf.lfHeight = m_dwFontSize;
  257.     lf.lfStrikeOut = 1;
  258.     lstrcpy(lf.lfFaceName, m_strFont);
  259.     hFont = CreateFontIndirect(&lf);
  260.     hFontOld = (HFONT)SelectObject(hdc, hFont); 
  261.     if( FAILED( hr = D3DXCreateText(m_pd3dDevice, hdc, _T("D3DXCreateText"), 
  262.         0.001f, 0.4f, &m_pMesh3DText, NULL, NULL) ) )
  263.     {
  264.         SelectObject(hdc, hFontOld);
  265.         return hr;
  266.     }
  267.     SelectObject(hdc, hFontOld);
  268.  
  269.     return S_OK;
  270. }
  271.  
  272.  
  273.  
  274.  
  275. //-----------------------------------------------------------------------------
  276. // Name: InvalidateDeviceObjects()
  277. // Desc: Called when the app is exiting, or the device is being changed,
  278. //       this function deletes any device dependent objects.
  279. //-----------------------------------------------------------------------------
  280. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  281. {
  282.     m_pFont->InvalidateDeviceObjects();
  283.     m_pStatsFont->InvalidateDeviceObjects();
  284.  
  285.     SAFE_RELEASE( m_pMesh3DText );
  286.     return S_OK;
  287. }
  288.  
  289.  
  290.  
  291.  
  292. //-----------------------------------------------------------------------------
  293. // Name: DeleteDeviceObjects()
  294. // Desc: Called when the app is exiting, or the device is being changed,
  295. //       this function deletes any device dependent objects.
  296. //-----------------------------------------------------------------------------
  297. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  298. {
  299.     m_pFont->DeleteDeviceObjects();
  300.     m_pStatsFont->DeleteDeviceObjects();
  301.  
  302.     return S_OK;
  303. }
  304.  
  305.  
  306.  
  307.  
  308. //-----------------------------------------------------------------------------
  309. // Name: FinalCleanup()
  310. // Desc: Called before the app exits, this function gives the app the chance
  311. //       to cleanup after itself.
  312. //-----------------------------------------------------------------------------
  313. HRESULT CMyD3DApplication::FinalCleanup()
  314. {
  315.     SAFE_DELETE( m_pFont );
  316.     SAFE_DELETE( m_pStatsFont );
  317.  
  318.     return S_OK;
  319. }
  320.  
  321.  
  322.  
  323.  
  324. //-----------------------------------------------------------------------------
  325. // Name: MsgProc()
  326. // Desc: Message proc function to handle key and menu input
  327. //-----------------------------------------------------------------------------
  328. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  329.                                     LPARAM lParam )
  330. {
  331.     if( WM_COMMAND == uMsg )
  332.     {
  333.         switch( LOWORD(wParam) )
  334.         {
  335.             case IDM_FONT:
  336.             {
  337.                 HDC hdc;
  338.                 LONG lHeight;
  339.                 hdc = GetDC( hWnd );
  340.                 lHeight = -MulDiv( m_dwFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72 );
  341.                 ReleaseDC( hWnd, hdc );
  342.                 hdc = NULL;
  343.  
  344.                 LOGFONT lf;
  345.                 lstrcpy( lf.lfFaceName, m_strFont );
  346.                 lf.lfHeight = lHeight;
  347.  
  348.                 CHOOSEFONT cf;
  349.                 ZeroMemory( &cf, sizeof(cf) );
  350.                 cf.lStructSize = sizeof(cf);
  351.                 cf.hwndOwner   = m_hWnd;
  352.                 cf.lpLogFont   = &lf;
  353.                 cf.Flags       = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
  354.                 
  355.                 if( ChooseFont( &cf ) )
  356.                 {
  357.                     SAFE_DELETE( m_pFont );
  358.                     lstrcpy( m_strFont, lf.lfFaceName );
  359.                     m_dwFontSize = cf.iPointSize / 10;
  360.  
  361.                     m_pFont = new CD3DFont( m_strFont, m_dwFontSize );
  362.  
  363.                     m_pFont->InitDeviceObjects( m_pd3dDevice );
  364.  
  365.                     m_pFont->RestoreDeviceObjects();
  366.  
  367.                     CreateD3DXTextMesh();
  368.                 }
  369.                 break;
  370.             }
  371.         }
  372.     }
  373.  
  374.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  375. }
  376.  
  377.  
  378.  
  379.